package com.heima.article.service.impl;

import com.heima.api.BehaviorClient;
import com.heima.api.UserClient;
import com.heima.article.mapper.ApArticleCollectionMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.LoadArticleService;
import com.heima.model.article.dtos.ArticleBehaviorDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.BeBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.utils.common.APUserThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoadArticleServiceImpl implements LoadArticleService {

    @Autowired
    private ApArticleCollectionMapper apArticleCollectionMapper;
    @Autowired
    private UserClient userClient;
    @Autowired
    private BehaviorClient behaviorClient;


    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDto dto) {

        Integer userId = APUserThreadLocalUtil.getUser();
        boolean isfollow = false, islike = false, isunlike = false, iscollection = false;
        //是否关注
        ApUserFollow follow = userClient.getFollowById(dto.getAuthorId());
        if (follow != null) {
            isfollow = true;
        }
        //是否收藏
        ApCollection collection = apArticleCollectionMapper.selectById(userId);
        if (collection != null) {
            iscollection = true;
        }
        //喜欢行为
        BeBehavior beBehavior = behaviorClient.findById(dto.getArticleId(), userId);

        if (beBehavior != null) {
            islike = true;
        }else {
            isunlike = true;
        }


        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isfollow", isfollow);
        resultMap.put("islike", islike);
        resultMap.put("isunlike", isunlike);
        resultMap.put("iscollection", iscollection);

        return ResponseResult.okResult(resultMap);
    }

}
